<template>
	<uv-popup ref="popupRef" round="20rpx" :closeOnClickOverlay="false" @change="change">
		<view class="delete-account-pop-box">
			<text class="delete-account-pop-title">注销确认</text>
			<text class="delete-account-pop-subtitle">请确认并输入要注销账号的信息</text>
			<view class="delete-account-pop-form">
				<!-- 身份证号 -->
				<xh-input placeholder="请输入注销的身份证号(企业输法人)" fontSize="26rpx" v-model="formParams.idCard" padding="0"></xh-input>
				<!-- 手机号 -->
				<view class="delete-account-pop-tel-box">
					<text>{{userStore.user.tel}}</text>
					<view class="line"></view>
				</view>
				<xh-input placeholder="请输入验证码" keyboardType="number" fontSize="26rpx" v-model="formParams.code" :maxCount="6" padding="0">
					<template #right>
						<text class="code-btn" :class="[isStartTime ? 'code-btn-select' : '']" @tap="startSendSms">{{isStartTime ? `${seconds}秒后重试` : '获取验证码'}}</text>
					</template>
				</xh-input>
			</view>
			<view class="bottom-box">
				<view class="bottom-left-btn" @tap="submit">确认注销</view>
				<view class="bottom-right-btn" @tap="popupRef.close()">取消</view>
			</view>
		</view>
	</uv-popup>
</template>

<script setup>
	import { ref, onUnmounted } from 'vue'
	import { totalSeconds } from '@/common/config/config.js'
	import { useUserStore } from '@/common/stores'
	import { sendCaptcha, accountDelete } from '@/common/api/api.js'
	const userStore = useUserStore()
	//是否开始倒计时
	const isStartTime = ref(false)
	// 倒计时秒数
	const seconds = ref(totalSeconds)
	const popupRef = ref(null)
	//计时器
	let timer = null
	const formParams = ref({
		idCard: '',
		code: ''
	})
	const show = () => {
		popupRef.value.open()
	}
	// 发送验证码
	const startSendSms = async () => {
		if (isStartTime.value) {
			return
		}
		let params = {}
		params.phoneNumber = userStore.user.tel
		params.smsType = 'cancel'
		await sendCaptcha(params)
		startTimer()
	}
	//开始倒计时
	const startTimer = () => {
		isStartTime.value = true
		//每秒执行一次
		timer = setInterval(() => {
			seconds.value -= 1
			if (seconds.value == 0) {
				seconds.value = totalSeconds
				isStartTime.value = false
				clearInterval(timer)
			}
		}, 1000)
	}
	const change = (e) => {
		if (!e.show) {
			if (timer) {
				clearInterval(timer)
				timer = null
				isStartTime.value = false
			}
		}
	}
	// 确认
	const submit = async () => {
		if (uni.$uv.test.empty(formParams.value.idCard)) {
			uni.$utils.showToast('请输入注销账号的身份证号码')
			return
		} else if (uni.$uv.test.empty(formParams.value.code)) {
			uni.$utils.showToast('请输入验证码')
			return
		}
		let params = {}
		params.certNo = uni.$uv.trim(formParams.value.idCard, 'all')
		params.certNo = formParams.value.code
		await accountDelete(params)
		uni.$utils.showToast('注销成功', 1000, () => {
			uni.$auth.logout()
		})
	}
	onUnmounted(()=>{
		if (timer) {
			clearInterval(timer)
		}
	})
	defineExpose({
		show
	})
</script>

<style lang="scss" scoped>
	.delete-account-pop-box {
		width: 600rpx;
		background-color: white;
		padding: 40rpx 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.delete-account-pop-subtitle {
			font-size: 24rpx;
			color: $xh-color-6;
			margin-top: 16rpx;
		}

		.delete-account-pop-form {
			width: 100%;
			padding: 30rpx 30rpx 0;

			.delete-account-pop-tel-box {
				display: flex;
				align-items: center;
				height: 100rpx;
				position: relative;
				color: $xh-color-9;
				font-size: 26rpx;

				.delete-account-pop-tel-line {
					transform: scaleY(1.0);
				}
			}

			.code-btn {
				color: $xh-theme-color;
				margin-left: 10rpx;
				font-size: 26rpx;
			}

			.code-btn-select {
				color: #cccccc;
			}
		}

		.bottom-box {
			display: flex;
			align-items: center;
			justify-content: space-around;
			margin-top: 30rpx;
			width: 100%;

			.bottom-left-btn {
				background-color: $xh-theme-color;
				height: 72rpx;
				line-height: 72rpx;
				padding: 0 50rpx;
				color: #ffffff;
				border-radius: 36rpx;
				font-size: 26rpx;
			}

			.bottom-right-btn {
				border: 1px solid $xh-border-color;
				height: 72rpx;
				line-height: 72rpx;
				padding: 0 50rpx;
				color: $xh-color-3;
				border-radius: 36rpx;
				font-size: 26rpx;

			}
		}
	}
</style>